package com.common.wx;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * 微信第三方登录工具类
 * @author wmm
 * 2019-03-09
 */
public class WXLoginUtil {
    /**
     * 拉起授权登录页
     * @param iwxapi
     */
    public static void login(IWXAPI iwxapi){
        SendAuth.Req   req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "login" + System.currentTimeMillis();
        req.transaction = "login";
        iwxapi.sendReq(req);
    }
    /**
     * 登陆后用code获取openId 和access_token
     * @param appId
     * @param secret
     * @param code
     * @param handler
     */
    public static void getOpenId(String appId, String secret, String code, final WxHandlerListener handler) {
        RequestParams params = new RequestParams();
        HttpUtils.get("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId +"&secret="+secret+"&code=" + code + "&grant_type=authorization_code", params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            }
            @Override
            public void onFinish() {
                super.onFinish();
                handler.finish();
            }
            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                if (responseString.contains("errcode")) {
                    handler.loginError();
                    return;
                }
                try {
                    JSONObject object = new JSONObject(responseString);
                    handler.getOpenidSuccess(object.getString("openid"),object.getString("access_token"));
                } catch (JSONException e) {
                    handler.loginError();
                }
            }
            @Override
            public void onStart() {
                super.onStart();
                handler.start();
            }
        });
    }

    /**
     * 获取微信个人信息 默认返回头像、姓名
     * @param openId
     * @param access_token
     * @param handler
     */
    public static void getWxInfo(final String openId, String access_token,final WxHandlerListener handler){
        RequestParams params = new RequestParams();
        HttpUtils.get("https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openId, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            }

            @Override
            public void onFinish() {
                super.onFinish();
                handler.finish();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                if (responseString.contains("errcode")) {
                    handler.loginError();
                    return;
                }
                try {
                    final JSONObject object = new JSONObject(responseString);
                    final String nickname = object.getString("nickname");
                    final String avatar = object.getString("headimgurl");
                    handler.getPersonalInfoSuccess(avatar,nickname);
                } catch (JSONException e) {
                    e.printStackTrace();
                    handler.loginError();
                }
            }

            @Override
            public void onStart() {
                super.onStart();
                handler.start();
            }
        });
    }
}
